getSpecialTypeId();

function getSpecialTypeId() {
    $.ajax({
        url: root.goodsInter.goods_getSpecialSkuTypes,
        cache: false, //禁用缓存
        type: "POST",
        success: function (data) {
            if (data.errorCode == 0) {
                var specialSkuTypesHtml = '<option value="" selected>不限</option><option value="-1">正常商品</option>';
                for (var i = 0; i < data.data.length; i++) {
                    specialSkuTypesHtml += '<option value="' + data.data[i].id + '">' + data.data[i].name + '</option>';
                }
                $(".specialTypeId").html(specialSkuTypesHtml);
            }
        }
    });
}

var dataSource = new Array();
//table的数据源
var table; //table对象
dataSource = [];

window.onload = function(){
    sessionStorage.setItem("wareHouseCode", 1)
}

function goodsList(data) {
    console.log(_categoryData)
    console.log(data)
    if(dataSource.length>0){
        dataSource.splice(0, dataSource.length); //清空数组
    }
    if(data.data.list!=null){
        $.each(data.data.list, function(i, field) {
            var rowData = new Array();
            rowData.push(field.sku_code);//code码
            rowData.push(field.img_url);//图片
            rowData.push(field.sku_name); //商品名称
            rowData.push(dealNull(field.specifications)); //规格属性
            rowData.push(field.brand_name);//品牌
            rowData.push(field.actual_stock);//可用库存（实际库存）
            rowData.push(field.virtual_stock);//可超卖库存（虚拟库存）
            rowData.push(field.virtual_stock_limit);//(总超卖库存)虚拟库存上限
            rowData.push(field.stockAttribute);//存量属性
            rowData.push(field.upper_stock);//库存上限
            rowData.push(field.arrival_cycle);//到货周期
            rowData.push(field.specialTypeId);
            rowData.push(field.isSpecialCommodity);
            dataSource.push(rowData);
            console.log(dataSource);
        });
    }
    initDataTable(dataSource);

    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable( ); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "autoWidth": false,
            "ordering": true,
            "searching": false,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aaSorting": [[7,'asc'],],//数据排序
            "aLengthMenu":[10,20,30],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function(data, type, row, meta) {
                        var val = '<img onclick="picShow(\'查看图片\',\'../goods/picShow.html\',\'' + row[1] + '\')" title="" width="50" height="50" src="' + row[1] + '" id="myImg" class="mr-10">';
                        return val;
                    },
                    "targets": 1
                },
                {
                    /*虚拟可超卖库存*/
                    "render": function (data, type, row, meta) {
                        if (row[6] == null) {
                            var val = '<span class="virtual_stock">0</span>';
                            return val;
                        } else {
                            var val = '<span class="virtual_stock">' + row[6] + '</span>';
                            return val;
                        }

                    },
                    "targets": 6
                },
                {
                    /*虚拟库存上限*/
                    "render": function(data, type, row, meta) {
                        var isSpecialSku = row[12]==true?true:false ;
                        var classStyle = isSpecialSku?"Hui-iconfont f-ricon-edit powerButton 1000001004725699 hide":"Hui-iconfont f-ricon-edit powerButton 1000001004725699" ;
                        if(row[7] == null){
                            var val = '<span class="virtual_stock_limit">' + row[7] + '</span>&nbsp;<input value="9999999" class="virtual_stock_inp hide">' +
                                '<span class="editor_btn '+classStyle+'" onclick="edit_stock(this,'+ row[11] +')">&#xe6df; </span>  ' +
                                '<button class="change_btn" type="button" onclick="isChange(this,\''+ row[0] +'\')" class="hide" >确定</button>' +
                                '<button class="cancel_btn"  type="button"  class="hide" onclick="isCancel(this)">取消</button>';
                            return val;
                        } else {
                            var val = '<span class="virtual_stock_limit">' + row[7] + '</span>&nbsp;<input value="' + row[7] + '" class="virtual_stock_inp hide">' +
                                '<span class="editor_btn '+classStyle+'" onclick="edit_stock(this,'+ row[11] +')">&#xe6df;</span>' +
                                '<button class="change_btn hide" type="button" onclick="isChange(this,\''+ row[0] +'\')">确定</button> ' +
                                '<button class="cancel_btn hide" type="button" onclick="isCancel(this)">取消</button>';
                            return val;
                        }
                    },
                    "targets": 7
                }
            ],
        });
        power();
    };
}
/*点击查看大图*/
function picShow(title, url, imgUrl) {
    window.event ? window.event.cancelBubble = true : e.stopPropagation();
    sessionStorage.setItem("imgUrl", imgUrl);
    layer_show(title, url, "800", "500");
}

/*表单验证特殊字符*/
function ValidateValue(textbox) {
    var IllegalString = "[`~!#$^&*()=|{}':;',\\[\\].<>/?~！#￥……&*（）——|{}【】‘；：”“'。，、？]‘’";
    var textboxvalue = textbox.value;
    var index = textboxvalue.length - 1;
    var s = textbox.value.charAt(index);
    if(IllegalString.indexOf(s) >= 0) {
        s = textboxvalue.substring(0, index);
        textbox.value = s;
        layer.msg("不能输入特殊字符");
    }
}
//可超卖库存修改
function edit_stock(el, specialTypeId) {
    if(specialTypeId == 1 || specialTypeId == 2 || specialTypeId == 3){
        $.Huimodalalert('淘汰/积压促销/分货销售商品，不支持修改', 2000);
        return;
    }
    let parentTd = $(el).parent();
    parentTd.find(".virtual_stock_limit").hide();
    parentTd.find(".editor_btn").hide();
    parentTd.find(".virtual_stock_inp").show();
    parentTd.find(".change_btn").show();
    parentTd.find(".cancel_btn").show();
}
function isChange(el, skuCode) {
    isCancel(el);
    console.log(skuCode);
    let parentTd = $(el).parent();
    let virtualStock = parentTd.find(".virtual_stock_inp").val();
    if (Number(virtualStock) > 9999999){
        $.Huimodalalert("总超卖库存最大为9999999!", 2000);
        return;
    }
    var stock_data = {
        skuCode: skuCode,
        warehouseId: $('#city_select').val(),
        VirtualStock: virtualStock,
    }
    var succCallBack = function (data, status, response) {
        if(data.errorCode==0){
            parentTd.find(".virtual_stock_limit").html(virtualStock);
            parentTd.parents("tr").find(".virtual_stock").html(virtualStock)
        }else {
            $.Huimodalalert(data.errorMessage, 2000);
        }
    };
    return web.doAjax(root.goodsInter.goods_updateVirtualStockValue, 'post', stock_data, succCallBack);
}
function isCancel(el) {
    let parentTd = $(el).parent();
    parentTd.find(".virtual_stock_limit").show();
    parentTd.find(".editor_btn").show();
    parentTd.find(".virtual_stock_inp").hide();
    parentTd.find(".change_btn").hide();
    parentTd.find(".cancel_btn").hide();
}